package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.FollowUser;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.service.IdService;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;


    /**
     * 保存小视频
     *
     * @param video
     * @return
     */
    @Override
    public String saveVideo(Video video) {

        //非空处理
        if (video.getUserId() == null) {
            return null;
        }

        video.setId(ObjectId.get());   //主键id
        video.setCreated(System.currentTimeMillis());
        //自增长id
        video.setVid(idService.createId("video", video.getId().toHexString()));
        //添加进mongodb中
        mongoTemplate.save(video);
        return video.getId().toHexString();
    }

    /**
     * 分页查询小视频,按照时间倒叙排序
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Video> queryVideoList(Integer page, Integer pageSize) {
        //构建分页查询条件
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = new Query().with(pageable);
        //查询
        List<Video> videos = this.mongoTemplate.find(query, Video.class);
        //封装到pageinfo对象
        PageInfo<Video> pageInfo = new PageInfo<>();
        pageInfo.setRecords(videos);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(0); //不提供总数
        //返回
        return pageInfo;
    }

    /**
     * 关注用户
     *
     * @param userId       用户id
     * @param followUserId 关注的用户id
     * @return
     */
    @Override
    public Boolean followUser(Long userId, Long followUserId) {
        //创建FollowUser对象
        try {
            FollowUser followUser = new FollowUser();
            followUser.setUserId(userId);  //用户id
            followUser.setFollowUserId(followUserId);  //关注的用户id
            followUser.setCreated(System.currentTimeMillis());  //关注时间
            followUser.setId(ObjectId.get());  //注解id
            mongoTemplate.save(followUser);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 取消关注用户
     *
     * @param userId       用户id
     * @param followUserId 关注的用户id
     * @return
     */
    @Override
    public Boolean disFollowUser(Long userId, Long followUserId) {
        //创建查询条件,再进行删除
        Query query = Query.query(Criteria.where("userId").is(userId).and("followUserId").is(followUserId));
        DeleteResult remove = mongoTemplate.remove(query, FollowUser.class);
        return remove.getDeletedCount() > 0;
    }

    /**
     * 根据id查询小视频
     *
     * @param videoId
     * @return
     */
    @Override
    public Video queryVideoById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId), Video.class);
    }


    /**
     * 根据vids查询视频
     *
     * @param vidList
     * @return
     */
    @Override
    public List<Video> queryVideoListByPids(List<Long> vidList) {

        Query query = Query.query(Criteria.where("vid").in(vidList));
        return mongoTemplate.find(query, Video.class);
    }
}